<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<title>transportTrace管理</title>
<jsp:include page="../inc.jsp"></jsp:include>

<c:if test="${fn:contains(sessionInfo.resourceList, '/traceController/delete')}">
	<script type="text/javascript">
		$.canDelete = true;
	</script>
</c:if>


<script type="text/javascript">
	var dataGrid;
	var Id = '<%= request.getParameter("id")%>';
	$(function() {
		dataGrid = $('#dataGrid').datagrid({
			url :'${pageContext.request.contextPath}/traceController/dataGrid',
			fit : true,
			striped : true,
			border : false,
			pagination : true,
			idField : 'id',
			fitColumns : false,
			pageSize : 30,
			pageList : [ 10, 20, 30, 40, 50 ],
			sortName : 'id',
			sortOrder : 'asc',
			checkOnSelect : true,
			selectOnCheck : true,
			queryParams: {
				transportId: Id,
			},
			nowrap : false,
			frozenColumns : [ [ {
				field : 'id',
				title : '序号',
				width : '10',
				checkbox : true
			} ] ],
			
			columns : [ [  
			{
				field : 'transportId',
				title : '订单序号',
				width : '30',
				hidden:true
			},
			
			{
				field : 'traceTime',
				title : '时间',
				width : '150',
				sortable : true 
			},{
				width : '300',
				title : '内容',
				field : 'remark',
				
		    	},{
						field : 'action',
						title : '操作',
						width : 100,
						formatter : function(value, row, index) {
							var str = '';
							str += '&nbsp;';
							if ($.canDelete) {
								str += $.formatString('<img onclick="deleteFun(\'{0}\');" src="{1}" title="删除"/>', row.id, '${pageContext.request.contextPath}/style/images/extjs_icons/cancel.png');
							}
						
							return str;
						}
					}] ],
			toolbar : '#toolbar',

			onLoadSuccess : function() {
			
				parent.$.messager.progress('close');

				$(this).datagrid('tooltip');
			},
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll').datagrid('uncheckAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
	});

	function deleteFun(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {//点击操作里面的删除图标会触发这个
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.messager.confirm('询问', '您是否要删除当前选中的追踪信息？', function(b) {
			if (b) {
				var currentUserId = '${sessionInfo.id}';/*当前登录用户的ID*/
				if (currentUserId != id) {
					parent.$.messager.progress({
						title : '提示',
						text : '数据处理中，请稍后....'
					});
					$.post('${pageContext.request.contextPath}/traceController/delete', {
						id : id
					}, function(result) {
						if (result.success) {
							parent.$.messager.alert('提示', result.msg, 'info');
							dataGrid.datagrid('reload');
						}
						parent.$.messager.progress('close');
					}, 'JSON');
				} else {
					parent.$.messager.show({
						title : '提示',
						msg : '不可以删除自己！'
					});
				}
			}
		});
	}

	
	
	function batchDeleteFun() {
		var rows = dataGrid.datagrid('getChecked');
		var ids = [];
		if (rows.length > 0) {
			parent.$.messager.confirm('确认', '您是否要删除当前选中的追踪信息？', function(r) {
				if (r) {
					parent.$.messager.progress({
						title : '提示',
						text : '数据处理中，请稍后....'
					});
					var currentUserId = '${sessionInfo.id}';/*当前登录用户的ID*/
					var flag = false;
					for ( var i = 0; i < rows.length; i++) {
						if (currentUserId != rows[i].id) {
							ids.push(rows[i].id);
						} else {
							flag = true;
						}
					}
					$.getJSON('${pageContext.request.contextPath}/traceController/batchDelete', {
						ids : ids.join(',')
					}, function(result) {
						if (result.success) {
							dataGrid.datagrid('load');
							dataGrid.datagrid('uncheckAll').datagrid('unselectAll').datagrid('clearSelections');
						}
						if (flag) {
							parent.$.messager.show({
								title : '提示',
								msg : '不可以删除自己！'
							});
						} else {
							parent.$.messager.alert('提示', result.msg, 'info');
						}
						parent.$.messager.progress('close');
					});
				}
			});
		} else {
			parent.$.messager.show({
				title : '提示',
				msg : '请勾选要删除的记录！'
			});
		}
	}





	function searchFun() {
		dataGrid.datagrid('load', $.serializeObject($('#searchForm')));
	}
	function cleanFun() {
		$('#searchForm input').val('');
		dataGrid.datagrid('load', {});
	}
</script>
</head>
<body>
	<div class="easyui-layout" data-options="fit : true,border : false">
	
		<div data-options="region:'center',border:false">
			<table id="dataGrid"></table>
		</div>
	</div>
	<div id="toolbar" style="display: none;">
		<c:if test="${fn:contains(sessionInfo.resourceList, '/traceController/batchDelete')}">
			<a onclick="batchDeleteFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'delete'">批量删除</a>
		</c:if>
	</div>


</body>
</html>
			<tr>
				<th style="width: 55px;">填报日期</th>
				<td><input class="easyui-datetimebox" name="traceTime"
					style="width:150px">
				</td>

			</tr>
			<tr>
				<th>跟踪内容</th>
				<td><textarea name="remark" class="easyui-validatebox"
					data-options="required:'true',missingMessage:'请填写跟踪内容'"
					style="
					height: 100px;" validType="length[0,200]"></textarea>
				</td>
			</tr>
		</table>
	</form>
</div>